<?php

namespace App\Http\Controllers\System;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;
use App\Http\ClassBox\globals;

class CContractController extends Controller
{
    public function index()
    {
        return view('system.cContract',[
            'pactList'=>DB::table('pact')->where('Type',0)->get()
            ]);
    }

    //form提交添加
    public function create(Request $request)
    {
        $data=unserialize($request->input('data'));
        //合同名称查重/不为空
        if(trim($data['Pact']['Name'])==NULL)//不能为空
        {
            return redirect('system/cContract/index')->with('callback', '合同名不能为空');
        }
        $check_name=DB::table('pact')->where('Name',trim($data['Pact']['Name']))->get();
        if($check_name!=NULL)//查重
        {
            return redirect('system/cContract/index')->with('callback', '合同名重复');
        }
        //写入pact
        $pactId=DB::table('pact')->insertGetId(['UseId'=>session('user')[0]->id,'Name'=>$data['Pact']['Name'],'Validity'=>$data['Pact']['Validity']]);
        //-----------写入pacttype开始-----------//
        $public_insert=array(
            'PactId'=>$pactId,
            'UseId'=>session('user')[0]->id
        );
        //设计
        $ued=(array_filter($data['PactType'], function($var){ return $var[0] == '2'; }));
        if($ued!=NULL)
        {
            $ued_key=array('Type','Name','UedWorktime','UedPrice','UedCommission');
            foreach($ued as $val)
            {
                $ued_insert[]=array_merge_recursive(array_combine($ued_key,$val),$public_insert);
            }
            DB::table('pacttype')->insert($ued_insert);
        }
        //开发
        $dev=(array_filter($data['PactType'], function($var){ return $var[0] == '4'; }));
        if($dev!=NULL)
        {
            $dev_key=array('Type','Name','KWorktime','KPrice','KCommission');
            foreach($dev as $val)
            {
                $dev_insert[]=array_merge_recursive(array_combine($dev_key,$val),$public_insert);
            }
            DB::table('pacttype')->insert($dev_insert);
        }
        //套餐
        $combo=(array_filter($data['PactType'], function($var){ return $var[0] == '6'; }));
        if($combo!=NULL)
        {
            $combo_key=array('Type','Name','UedWorktime','UedPrice','UedCommission','KWorktime','KPrice','KCommission');
            foreach($combo as $val)
            {
                $combo_insert[]=array_merge_recursive(array_combine($combo_key,$val),$public_insert);
            }
            DB::table('pacttype')->insert($combo_insert);
        }
        //-----------写入pacttype结束-----------//
        return redirect('system/cContract/index')->with('callback', '添加成功');
    }

    //排序功能
    public function sort(Request $request)
    {
        $up_inx=DB::table('pacttype')->where('id',$request->input('up_id'))->pluck('Inx');   
        $down_inx=DB::table('pacttype')->where('id',$request->input('down_id'))->pluck('Inx');
        if($up_inx==$down_inx)
        {
            DB::table('pacttype')->where('id',$request->input('up_id'))->increment('Inx');
            return 1;
        }
        else
        {
            $update_inx=$down_inx+1;
            DB::table('pacttype')->where('id',$request->input('up_id'))->update(['Inx'=>$update_inx]);
            return 2;
        }
        return 0;
    }

    //复制合同
    public function copy(Request $request)
    {
        //递归方法把对象转换为数组
        function objectToArray($e){
            $e=(array)$e;
            foreach($e as $k=>$v){
                if( gettype($v)=='resource' ) return;
                if( gettype($v)=='object' || gettype($v)=='array' )
                    $e[$k]=(array)objectToArray($v);
            }
            return $e;
        }
        //合同名称查重
        $check_name=DB::table('pact')->where('Name',trim($request->input('pact_name')))->get();
        if($check_name!=NULL)
        {
            return 0;
        }
        //复制合同
        $pact_mod=DB::table('pact')->select('Del','Name','Type','Validity')->where('id',$request->input('pact_mod_id'))->get();
        $pact_insert=array_merge_recursive(get_object_vars($pact_mod[0]),['UseId'=>session('user')[0]->id]);
        $pact_insert_id=DB::table('pact')->insertGetId($pact_insert);
        //更新合同名称
        DB::table('pact')->where('id',$pact_insert_id)->update(['Name'=>trim($request->input('pact_name'))]);
        //复制合同类型
        $pacttype_mod=DB::table('pacttype')->select('Type','Name','UedWorktime','UedPrice','UedCommission','KWorktime','KPrice','KCommission','Del','Inx')->where('PactId',$request->input('pact_mod_id'))->get(); 
        $change_type=objectToArray($pacttype_mod);
        foreach($change_type as $val)
        {
            $pacttype_insert[]=array_merge_recursive($val,['PactId'=>$pact_insert_id,'UseId'=>session('user')[0]->id]);
        }
        DB::table('pacttype')->insert($pacttype_insert);
        //写入完成，把新合同输出
        $data['Pact']=DB::table('pact')->select('id','Name','Validity','Del')->where('id',$pact_insert_id)->get();
        $data['PactType']=DB::table('pacttype')->select('id','Type','UedWorktime','UedPrice','UedCommission','KWorktime','KPrice','KCommission','Del','Inx','Name')->where('PactId',$pact_insert_id)->orderBy('Inx','desc')->get();
        $data['pact_list']=DB::table('pact')->get();
        return json_encode($data);
    }

    //合同修改请求
    public function edit(Request $request)
    {
        $data['Pact']=DB::table('pact')->select('id','Name','Validity','Del')->where('id',$request->input('pact_id'))->get();
        $data['PactType']=DB::table('pacttype')->select('id','Type','UedWorktime','UedPrice','UedCommission','KWorktime','KPrice','KCommission','Del','Inx','Name')->where('PactId',$request->input('pact_id'))->orderBy('Inx','desc')->get();
        return json_encode($data);
    }

    //合同修改form提交
    public function edit_form(Request $request)
    {
        $data=unserialize($request->input('data'));
        /*test
        // $data['Pact']=array('Name'=>'合同B','Validity'=>'2015-01-01,2016-10-10');
        // $data['PactType']=array(18=>array('2','设计1','10','1001','800'),19=>array('2','设计2','10','1002','800'),20=>array('4','开发','20','1200','800'),21=>array('6','套餐','10','1000','800','20','1200','800'));
        //$data['TypeAdd']=array();
        //dump($data);
        */
        //合同名称查重/不为空
        if(trim($data['Pact']['Name'])==NULL)//不能为空
        {
            return redirect('system/cContract/index')->with('callback', '合同名不能为空');
        }
        $check_name=DB::table('pact')->where('Name',trim($data['Pact']['Name']))->get();
        if($check_name!=NULL)//查重
        {
            return redirect('system/cContract/index')->with('callback', '合同名重复');
        }
        //写入pact
        $pact_key=$request->input('pact_id');
        $pactId=DB::table('Pact')->where('id',$pact_key)->update(array_merge($data['Pact'],['UseId'=>session('user')[0]->id]));
        //-----------写入pacttype开始-----------//
        $public_insert=array(
            'PactId'=>$pact_key,
            'UseId'=>session('user')[0]->id
        );
        //设计
        $ued=(array_filter($data['PactType'], function($var){ return $var[0] == '2'; }));
        if($ued!=NULL)
        {
            $ued_key=array('Type','Name','UedWorktime','UedPrice','UedCommission');
            foreach($ued as $key=>$val)
            {
                $ued_insert=array_merge_recursive(array_combine($ued_key,$val),$public_insert);
                //dump($ued_insert);
                //dump($key);
                DB::table('pacttype')->where('id',$key)->update($ued_insert);               
            }
        }
        //开发
        $dev=(array_filter($data['PactType'], function($var){ return $var[0] == '4'; }));
        if($dev!=NULL)
        {
            $dev_key=array('Type','Name','KWorktime','KPrice','KCommission');
            foreach($dev as $key=>$val)
            {
                $dev_insert=array_merge_recursive(array_combine($dev_key,$val),$public_insert);
                DB::table('pacttype')->where('id',$key)->update($dev_insert);
            }
        }
        //套餐
        $combo=(array_filter($data['PactType'], function($var){ return $var[0] == '6'; }));
        if($combo!=NULL)
        {
            $combo_key=array('Type','Name','UedWorktime','UedPrice','UedCommission','KWorktime','KPrice','KCommission');
            foreach($combo as $key=>$val)
            {
                $combo_insert=array_merge_recursive(array_combine($combo_key,$val),$public_insert);
                DB::table('pacttype')->where('id',$key)->update($combo_insert);
            }
        }
        //-----------提交修改添加新条目-------//
        //设计
        $uedAdd=(array_filter($data['TypeAdd'], function($var){ return $var[0] == '2'; }));
        if($uedAdd!=NULL)
        {
            $ued_key_add=array('Type','Name','UedWorktime','UedPrice','UedCommission');
            foreach($uedAdd as $val)
            {
                $ued_insert_add[]=array_merge_recursive(array_combine($ued_key_add,$val),$public_insert);
            }
            DB::table('pacttype')->insert($ued_insert_add);
        }
        //开发
        $devAdd=(array_filter($data['TypeAdd'], function($var){ return $var[0] == '4'; }));
        if($devAdd!=NULL)
        {
            $dev_key_add=array('Type','Name','KWorktime','KPrice','KCommission');
            foreach($devAdd as $val)
            {
                $dev_insert_add[]=array_merge_recursive(array_combine($dev_key_add,$val),$public_insert);
            }
            DB::table('pacttype')->insert($dev_insert_add);
        }
        //套餐
        $comboAdd=(array_filter($data['TypeAdd'], function($var){ return $var[0] == '6'; }));
        if($comboAdd!=NULL)
        {
            $combo_key_add=array('Type','Name','UedWorktime','UedPrice','UedCommission','KWorktime','KPrice','KCommission');
            foreach($comboAdd as $val)
            {
                $combo_insert_add[]=array_merge_recursive(array_combine($combo_key_add,$val),$public_insert);
            }
            DB::table('pacttype')->insert($combo_insert_add);
        }
        //-----------写入pacttype结束-----------//

        return redirect('system/cContract/index')->with('callback', '修改成功');
    }


    //合同/合同类型的启用或禁用
    public function destroy(Request $request)
    {
        if($request->input('del_type')==1)//合同启用或禁用
        {
            switch($request->input('del'))
            {
                case 1://禁用合同
                DB::table('pact')->where('id',$request->input('pact_id'))->update(['Del'=>0]);
                DB::table('pacttype')->where('PactId',$request->input('pact_id'))->update(['Del'=>0]);
                return 1;
                break;
                case 0://启用合同
                DB::table('pact')->where('id',$request->input('pact_id'))->update(['Del'=>1]);
                return 1;
                break;
                default:
                return 0;
            }
        }
        elseif($request->input('del_type')==0)//合同类型启用或禁用
        {
            switch($request->input('del'))
            {
                case 1://禁用合同类型
                DB::table('pacttype')->where('id',$request->input('pacttype_id'))->update(['Del'=>0]);
                return 1;
                break;
                case 0://启用合同类型
                DB::table('pacttype')->where('id',$request->input('pacttype_id'))->update(['Del'=>1]);
                return 1;
                break;
                default:
                return 0;
            }
        }
        else
        {
            return 0;
        }
    }

    public function test(Request $request)
    {
        
    }


}
